#include "PlaneFactory.h"
#include "CNumber.h"
#include "ObjectDataDefine.h"
#include "xml_tag.h"
#include "StrategyBank.h"
#include "BaseStrategy.h"




PlaneFactory::PlaneFactory(void)
{
	_id = NULL;
	_ai = NULL;
	_listLevel = NULL;
	_listType = NULL;
}


PlaneFactory::~PlaneFactory(void)
{
	CC_SAFE_RELEASE(_id);
	CC_SAFE_RELEASE(_ai);
	CC_SAFE_RELEASE(_listLevel);
	CC_SAFE_RELEASE(_listType);
}

bool PlaneFactory::init()
{
	//setWeapons(CCArray::create());
	setLevels(CCArray::create());
	setTypes(CCArray::create());

	return true;
}

void PlaneFactory::parseXml(xml_node node)
{
	char* id = (char*)node.child(XMLTAG_ID).text().as_string();
	setId(ccs(id));
	char* ai = (char*)node.child(XMLTAG_AI).text().as_string();
	setAi(ccs(ai));
	xml_node weapons = node.child(XLMTAG_LEVELS);
	for (xml_node i = weapons.child(XLMTAG_LEVEL); i; i = i.next_sibling(XLMTAG_LEVEL))
	{
		//_listWeapon->addObject(ccs(i.text().as_string()));
		char* type = (char*)i.child(XMLTAG_RESOURCE_TYPE).text().as_string();
		char* weapon = (char*)i.child(XLMTAG_WEAPON).text().as_string();
		int damage = i.child(XLMTAG_DAMAGE).text().as_int();
		int health = i.child(XLMTAG_HEALTH).text().as_int();
		int speed = i.child(XLMTAG_SPEED).text().as_int();
		int price = i.child(XMLTAG_PRICE).text().as_int();

		PlaneData* planeData = new PlaneData();
		planeData->autorelease();
		planeData->setType(ccs(type));
		planeData->setWeapon(ccs(weapon));
		planeData->setDamage(damage);
		planeData->setHealth(health);
		planeData->setSpeed(speed);
		planeData->setPrice(price);

		_listLevel->addObject(planeData);
	}

	xml_node types = node.child(XMLTAG_TYPES);
	for (xml_node i = types.child(XMLTAG_RESOURCE_TYPE); i; i = i.next_sibling(XMLTAG_RESOURCE_TYPE))
	{
		char* tId = (char*)i.child(XMLTAG_ID).text().as_string();
		char* spriteFrame = (char*)i.child(XMLTAG_RESOURCE_SPRITEFRAME).text().as_string();

		xml_node bound = i.child(XMLTAG_BOUND);
		int boundX = bound.child(XMLTAG_WIDTH).text().as_int();
		int boundY = bound.child(XMLTAG_HEIGHT).text().as_int();

		int positionX = i.child(XMLTAG_RESOURCE_POSITION).child(XMLTAG_X).text().as_int();
		int positionY = i.child(XMLTAG_RESOURCE_POSITION).child(XMLTAG_Y).text().as_int();
		float scaleX = i.child(XMLTAG_RESOURCE_SCALE).child(XMLTAG_X).text().as_float();
		float scaleY = i.child(XMLTAG_RESOURCE_SCALE).child(XMLTAG_Y).text().as_float();
		int levelFrom = i.child(XLMTAG_LEVELFROM).text().as_int();
		int levelTo = i.child(XLMTAG_LEVELTO).text().as_int();
		

		CCDictionary* type =CCDictionary::create();
		type->setObject(ccs(tId), XMLTAG_ID);
		type->setObject(ccs(spriteFrame), XMLTAG_RESOURCE_SPRITEFRAME);
		type->setObject(CCNumber<CCSize>::create(CCSizeMake(boundX, boundY)), XMLTAG_BOUND);
		type->setObject(CCNumber<CCPoint>::create(ccp(positionX, positionY)), XMLTAG_RESOURCE_POSITION);
		type->setObject(CCNumber<CCPoint>::create(ccp(scaleX, scaleY)), XMLTAG_RESOURCE_SCALE);
		type->setObject(CCNumber<int>::create(levelFrom), XLMTAG_LEVELFROM);
		type->setObject(CCNumber<int>::create(levelTo), XLMTAG_LEVELTO);

		_listType->addObject(type);
	}
}

PlayerPlane* PlaneFactory::createPlaneByType( char* type )
{
	CCObject* it, *it2;
	PlayerPlane* plane = NULL;
	CCARRAY_FOREACH(_listType, it)
	{
		CCDictionary* dtype = dynamic_cast<CCDictionary*>(it);
		char* tId = (char*)dynamic_cast<CCString*>(dtype->objectForKey(XMLTAG_ID))->getCString();
		if (strcmp(tId, type) == 0)
		{
			char* spriteFrame = (char*)dynamic_cast<CCString*>(dtype->objectForKey(XMLTAG_RESOURCE_SPRITEFRAME))->getCString();
			CCSize bound = dynamic_cast<CCNumber<CCSize>*>(dtype->objectForKey(XMLTAG_BOUND))->getNumber();
			CCPoint position = dynamic_cast<CCNumber<CCPoint>*>(dtype->objectForKey(XMLTAG_RESOURCE_POSITION))->getNumber();
			CCPoint scale = dynamic_cast<CCNumber<CCPoint>*>(dtype->objectForKey(XMLTAG_RESOURCE_SCALE))->getNumber();
			int levelFrom = dynamic_cast<CCNumber<int>*>(dtype->objectForKey(XLMTAG_LEVELFROM))->getNumber();
			int levelTo = dynamic_cast<CCNumber<int>*>(dtype->objectForKey(XLMTAG_LEVELTO))->getNumber();

			plane = PlayerPlane::create();
			CCSprite* context = CCSprite::createWithSpriteFrameName(spriteFrame);
			context->setAnchorPoint(CCPointZero);
			context->setPosition(position);
			context->setScaleX(scale.x);
			context->setScaleY(scale.y);
			plane->addChild(context);
			plane->setContentSize(bound);
			plane->setProtectBoundBox(CCSizeZero);



		}
	}

	BaseStrategy* ai = StrategyBank::sharedStrategyBank()->createStrategy(_ai->getCString());
	plane->addChild(ai);
	plane->setStrategy(ai);
	return plane;
}

PlayerPlane* PlaneFactory::createPlaneByLevel( int level )
{
	level--;
	if (level < 0)
		level = 0;
	else if (level >= _listLevel->count())
		level = _listLevel->count() - 1;

	PlaneData* planeData = dynamic_cast<PlaneData*>(_listLevel->objectAtIndex(level));
	PlayerPlane* plane = createPlaneByType((char*)planeData->getType()->getCString());

	CCArray* temp = plane->getSupportedWeaponKeys();
	CCObject* it;
	CCARRAY_FOREACH(_listLevel, it)
	{
		planeData = dynamic_cast<PlaneData*>(it);
		temp->addObject(planeData->getWeapon());
	}

	//plane->setWeaponLevel(level + 1);
	return plane;
}

PlaneData* PlaneFactory::getPlaneDataByLevel( int level )
{
	level--;
	if (level < 0)
		level = 0;
	else if (level >= _listLevel->count())
		level = _listLevel->count() - 1;

	PlaneData* planeData = dynamic_cast<PlaneData*>(_listLevel->objectAtIndex(level));
	return planeData;
}

int PlaneFactory::getPriceAtLevel( int level )
{
	level--;
	if (level < 0)
		level = 0;
	else if (level >= _listLevel->count())
		level = _listLevel->count() - 1;

	PlaneData* planeData = dynamic_cast<PlaneData*>(_listLevel->objectAtIndex(level));
	return planeData->getPrice();
}
